package servletpackage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import news.bll.comments.CommentsDaoImpl;
import news.bll.news.NewsDaoImpl;
import news.entity.Comment;
import news.entity.News;

public class ReadNewsServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 525675552528944184L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=UTF-8");
		int nid = Integer.parseInt(req.getParameter("nid"));
		NewsDaoImpl ndi = new NewsDaoImpl();
		News n = ndi.getANews(nid);
		req.setAttribute("news", n);
		CommentsDaoImpl cdi = new CommentsDaoImpl();
		List<Comment> comments = new ArrayList<Comment>();
		comments = cdi.getComments(nid);
		req.setAttribute("comments", comments);
		req.getRequestDispatcher("news_read.jsp?nid=" + nid).include(req, resp);
	}

}
